$(function(){
    // 检测登录状态
    chrome.extension.sendMessage({
        "data":'1',
        "type": 'islogin'
    }, function(response) {
        if (response.user){
            $("#form").hide();
            $("#user_info").show();
            $("#user_inf").html(response.user);
            localStorage.setItem('userName',response.user);
            if(response.active == 1){
                $("#user_active").html('已激活');
            }else{
                $("#user_active").css({'color':'blue'}).html('未激活，请联系管理员。');
                $("#logout").show();
            }
            if (response.expire>0){// 未过期
                $("#user_expire").html(formatDateTime(response.expire));
                $("#user_detail").css({'color':'blue'}).html('').html(response.msg);
                if (!$("#darenTips").is(":visible")){
                    $("#darenTips").show();
                    $("#tipsInfo").hide();
                }
                $("#logout").show();
                if (response.updateCookieTime > 0){
                    $("#updated").show();
                    $("#updateTime").html('').html(formatDateTime(response.updateCookieTime));
                }

            }else{// 已过期
                $("#user_expire").html('');
                $("#user_detail").css({'color':'red'}).html('').html(response.msg);
                $("#username").val(response.user);
                $("#login").parent().parent().show();
                $("#user_active").css({'color':'blue'}).html('');
                setTimeout(function(){
                    $("#user_info").hide();
                    $("#logout").hide();
                    $("#darenTips").hide();
                    $("#form").show();
                },3000);
                $("#logout").show();
            }

        }else{
            $("#user_info").hide();
            $("#form").show();
        }

    });
    //点击登录
    $("#submit").click(function(){
        var user = $("#username").val();
        var pwd = $("#password").val();
        var remember = $("#remember").prop("checked");
        if(user != '' && pwd != ''){
            var userObj = new Object();
            userObj.user = user;
            userObj.pwd = pwd;
            userObj.isRemember = remember;
            chrome.extension.sendMessage({
                "data":userObj,
                "type": 'login'
            }, function(response) {
                console.log(response);
                try {
                    if (response.status == 1){
                        $("#form").hide();// 表达登录页隐藏
                        $("#user_info").show();// 信息页显示
                        $("#user_inf").html(response.user);
                        $("#user_expire").html(formatDateTime(response.expire));
                        if(response.active == 1){
                            $("#user_active").html('已激活');
                        }else{
                            $("#user_active").css({'color':'blue'}).html('未激活，请联系管理员。');
                            $("#logout").show();
                        }
                        $("#user_detail").css({'color':'blue'}).html(response.msg);
                        $("#tipsInfo").hide();// tips隐藏
                        $("#darenTips").show();// 更新cookie显示
                    }else if(response.status == 0){
                        $("#tips").html('').html(response.msg).show();
                    }else if (response.status == 2){
                        $("#tips").html('').html(response.msg).show();
                    }else if(response.status == 3){
                        $("#tips").html('').html(response.msg).show();
                    }else{
                        $("#tips").html('').html('404 Not Found URL').show();
                    }
                } catch (e) {
                    //console.log("Server-Error")
                }
            });
            $("#tips").hide();
            $("#logout").show();
        }else{
            $("#tips").html('用户名或密码错误').show();
        }
    });

    // 退出
    $("#logout").click(function(){
        chrome.extension.sendMessage({
            "data":'1',
            "type": 'logout'
        }, function(response) {
            console.log(response);
            if (response.status == 1){
                $("#user_info").hide(200,function(){
                    $("#form").show(300);
                    $("#tipsInfo").show(500);
                    $("#darenTips").hide();
                    $("#logout").hide();
                    $("#username").val(localStorage.getItem("userName"));
                });
            }else{
                console.log('Logouit fail.');
            }
        });
    });
    //格式化时间戳
    function formatDateTime(timeStamp) {
        var date = new Date();
        date.setTime(timeStamp * 1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    };
});